package com.zq.it.action.exam;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

import com.zq.it.action.BaseAction;
import com.zq.it.dao.common.PageConfig;
import com.zq.it.pojo.Student;
import com.zq.it.pojo.exam.TPaper;
import com.zq.it.pojo.exam.data.ObjectData;
import com.zq.it.pojo.exam.data.PaperData;
import com.zq.it.tool.Constant;

public class ExamAction extends BaseAction {
	private Student student;
	private ObjectData objList;
	private PaperData paperData;
	private TPaper paper;
	private String msg;
	private Long[] ids;
	private Integer state; // 是否公布

	/**
	 * 显示我的所有考试
	 * 
	 * @return
	 */
	public String myExam() {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		PageConfig config = new PageConfig();
		config.setHql(
				"from TExamResult as t where t.student.id=" + student.getId());
		config.setCountHql(
				"select count(*) from TExamResult as t where t.student.id="
						+ student.getId());
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("exam_myExam.asp");
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		objList = examService.getMyExamList(config);
		return "myExam";
	}

	/**
	 * 开始我的考试详情
	 * 
	 * @return
	 */
	public String showMyPaperDetail() {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		paperData = paperService.getPaperData(student, ids[0]);
		long nowTime = new Date().getTime();
		if (paperData.getPaper().getStartTime().getTime() > nowTime
				|| paperData.getPaper().getEndTime().getTime() < nowTime) {
			return myExam();
		}
		return "showMyPaperDetail";
	}

	/**
	 * 获取考试剩余时间
	 * 
	 * @throws IOException
	 * 
	 */
	public void getLeftTime() throws IOException {
		Long leftTime = examService.getLeftTimeByPaperId(paper.getId());
		print_text_utf8(leftTime + "");
	}

	/**
	 * 提交试卷
	 * 
	 * @return
	 */
	public String postPaper() {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		paperData = paperService.getPaperData(student, ids[0]);
		if (null == paperData) {
			msg = "请操作自己的试卷，请求非法！";
			return "postPaperJump";
		}
		msg = examService.addValidateAndSubmitMyExam(ids[0], student, paperData,
				getRequest(), getResponse());
		return "postPaperJump";
	}

	/**
	 * 显示考试结果详情
	 * 
	 * @return
	 */
	public String showMyExamResult() {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		Map map = examService.getMyExamResult(ids[0], student);
		if (map == null) {
			msg = "权限不足！";
			return "postPaperJump";
		}
		if (!(Boolean) map.get("data_check")) {
			msg = "未提交试卷！";
			return "postPaperJump";
		}
		PaperData paperData = paperService.getPaperData(student, ids[0]);
		long nowTime = new Date().getTime();
		if (paperData.getPaper().getEndTime().getTime() > nowTime) {
			return myExam();
		}
		getRequest().setAttribute("paperData", paperData);
		getRequest().setAttribute("examResultDetail", map);
		return "showMyExamResult";
	}

	/**
	 * 查询我的考试信息
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String searchMyExam() throws UnsupportedEncodingException {
		student = (Student) getHttpSession().getAttribute(Constant.STUDENT);
		if (null != paper.getPName() && !paper.getPName().isEmpty()) {
			paper.setPName(URLDecoder.decode(paper.getPName(), Constant.UTF_8));
		}
		String hql = examService.creatMyExamHql(paper, state, student);
		PageConfig config = new PageConfig();
		config.setHql(hql);
		config.setCountHql("select count(*) " + hql);
		config.setPage(page);
		config.setPageSize(Constant.PAGE_SIZE);
		config.setUrl("exam_searchMyExam.asp");
		config.setExtendUrl(
				"paper.PName="
						+ URLEncoder.encode(URLEncoder.encode(paper.getPName(),
								Constant.UTF_8), Constant.UTF_8)
						+ "&state=" + state);
		config.setPerPageCount(Constant.PER_PAGE_COUNT);
		objList = examService.getMyExamList(config);
		return "myExam";
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public ObjectData getObjList() {
		return objList;
	}

	public void setObjList(ObjectData objList) {
		this.objList = objList;
	}

	public PaperData getPaperData() {
		return paperData;
	}

	public void setPaperData(PaperData paperData) {
		this.paperData = paperData;
	}

	public TPaper getPaper() {
		return paper;
	}

	public void setPaper(TPaper paper) {
		this.paper = paper;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}
}
